import Vue from 'vue'
import Vuex from 'vuex'
import router from '../router'
// import users from './module/users'
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    users:[],
    isLogin:false,
    nowUser:""
  },
  mutations: {
    addUser(state,data){
     
      let isExit=false;
      // state.users.push(data)
      isExit=(state.users.some(function(item){
        return item.uname==data.uname
      }));

      if(state.users.length==0){
        alert("注册成功")
        state.users.push(data)
        router.push(`/login`)
      }else{
        if(isExit){
          alert("该用户已存在")
        }else{
          alert("注册成功")
          state.users.push(data)
          router.push(`/login`)
        }
      }

      console.log(state.users);
    },

    checkLogin(state,data){
      state.isLogin=false
      state.isLogin=state.users.some(function(item){
        return item.uname==data.uname&&item.password==data.password
      })
      if(state.isLogin){
        alert("登录成功,即将跳转到首页")
        state.nowUser=data.uname
        router.push(`/`)
      }else{
        alert("登录失败~")
      }
      
    }
  },
  actions: {
  },
  modules: {
    // users
  },
  // plugins: [createPersistedState()] //默认存在localstorage中

  plugins: [createPersistedState({ //若要存在session中
    storage:window.sessionStorage
  })]

})
